$(function(){
    /* for dutyTime */
    $('.worktimeH, .worktimeM').blur(function(){
        var $parent = $(this).parent();
        var worktimeH = $parent.find('.worktimeH').val();
        var worktimeM = $parent.find('.worktimeM').val();
        if ( worktimeH.length == 1 ) {
            worktimeH += '0'+worktimeH;
        }
        worktimeM = (worktimeM.length==1) ? '0'+worktimeM : worktimeM;
        $parent.find('.worktime').val( worktimeH + worktimeM );
    }).keydown(function(e){
//        if ( (e.keyCode >= 49 && e.keyCode <= 57) || (e.keyCode >=96 && e.keyCode <=105) || e.keyCode ==8 || e.keyCode ==9 || e.keyCode ==46 || (e.keyCode >= 37 && e.keyCode <=40)) {
        if ( (e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >=96 && e.keyCode <=105) || e.keyCode ==8 || e.keyCode ==9 || e.keyCode ==46 || (e.keyCode >= 37 && e.keyCode <=40)) {
            return true;
        } else {
            return false;
        }
    });

    /* for otherContent*/
    $('.otherContentList').change(function(){
        window.console && console.log($(this).prev());
        $(this).prev().val(this.value);
    });

    $('.offtimeH, .offtimeM').blur(function(){
        var $parent = $(this).parent();
        var offtimeH = $parent.find('.offtimeH').val();
        var offtimeM = $parent.find('.offtimeM').val();

        offtimeH = (offtimeH.length==1) ? '0'+offtimeH : offtimeH;
        offtimeM = (offtimeM.length==1) ? '0'+offtimeM : offtimeM;
        $parent.find('.offtime').val( offtimeH + offtimeM );
    }).keydown(function(e){
//        if ( (e.keyCode >= 49 && e.keyCode <= 57) || (e.keyCode >=96 && e.keyCode <=105) || e.keyCode ==8 || e.keyCode ==9 || e.keyCode ==46 || (e.keyCode >= 37 && e.keyCode <=40)) {
        if ( (e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >=96 && e.keyCode <=105) || e.keyCode ==8 || e.keyCode ==9 || e.keyCode ==46 || (e.keyCode >= 37 && e.keyCode <=40)) {
            return true;
        } else {
            return false;
        }
    });


    /* for outTime */
    $('.writeImg').click(function(){
        var $parentTr = $(this.parentNode.parentNode);
        var imgIndex = this.getAttribute('imgindex');
        $this = $(this);
        $parentTr.find('.kinContont input:first').val('true');

        // has no data
        if ( $this.hasClass('noData') ) {
            var displayElm = $parentTr.find('.kinContont, .zhenxiuTd, .dropdownListTd, .workdayCommonTd, .workdayOverNightTd, .holidayCommonTd, .holidayOverNightTd');
            for (var i=0; i<displayElm.length; i++) {
                displayElm[i].style.visibility = '';
            }
            var displayElm = $parentTr.find('.huriymd_inp, .kinContont_inp');
            for (var i=0; i<displayElm.length; i++) {
                displayElm[i].style.display = 'none';
            }
            var displayElm = $parentTr.find('.huriymd_dsp, .kinContont_dsp');
            for (var i=0; i<displayElm.length; i++) {
                displayElm[i].style.display = 'block';
            }
        } else if ( $this.hasClass('hasData') ) {
            var displayElm = $parentTr.find('.kinContont,.zhenxiuTd, .dropdownListTd');
            for (var i=0; i<displayElm.length; i++) {
                displayElm[i].style.visibility = '';
            }
            var displayElm = $parentTr.find('.huriymd_inp, .kinContont_inp');
            for (var i=0; i<displayElm.length; i++) {
                displayElm[i].style.display = 'block';
            }
            var displayElm = $parentTr.find('.huriymd_dsp, .kinContont_dsp');
            for (var i=0; i<displayElm.length; i++) {
                displayElm[i].style.display = 'none';
            }

            var workdayCommonTd = $parentTr.find('.workdayCommonTd');
            var workdayOverNightTd = $parentTr.find('.workdayOverNightTd');
            var holidayCommonTd = $parentTr.find('.holidayCommonTd');
            var holidayOverNightTd = $parentTr.find('.holidayOverNightTd');
            var tds = [workdayCommonTd,workdayOverNightTd, holidayCommonTd, holidayOverNightTd];
            $(tds).each(function(index,elm){
                var spanF = ($(this).find('span:first'))[0];
                var timeArr = ['',''];
                var minuteTime = '';
                if (spanF) {
                    var time = spanF.innerHTML;
                    timeArr = time.split(':');
                    minuteTime = timeArr[0]*60 + timeArr[1]*1;
                }
                switch (index) {
                    case 0:
                        var inputHidden = '<input class="workdayCommon" name="workdayCommon'+imgIndex+'" type="hidden" value="'+minuteTime+'" />';
                        var inputTextH = '<input class="workdayCommonH" size="2" maxlength="2" style="text-align:right;" type="text" value="'+timeArr[0]+'"/>';
                        var inputTextM = '<input class="workdayCommonM" size="2" maxlength="2" style="text-align:right;" type="text" value="'+timeArr[1]+'"/>';
                        break;
                    case 1:
                        var inputHidden = '<input class="workdayOverNight" name="workdayOverNight'+imgIndex+'" type="hidden" value="'+minuteTime+'" />';
                        var inputTextH = '<input class="workdayOverNightH" size="2" maxlength="2" style="text-align:right;" type="text" value="'+timeArr[0]+'"/>';
                        var inputTextM = '<input class="workdayOverNightM" size="2" maxlength="2" style="text-align:right;" type="text" value="'+timeArr[1]+'"/>';
                        break;
                    case 2:
                        var inputHidden = '<input class="holidayCommon" name="holidayCommon'+imgIndex+'" type="hidden" value="'+minuteTime+'" />';
                        var inputTextH = '<input class="holidayCommonH" size="2" maxlength="2" style="text-align:right;" type="text" value="'+timeArr[0]+'"/>';
                        var inputTextM = '<input class="holidayCommonM" size="2" maxlength="2" style="text-align:right;" type="text" value="'+timeArr[1]+'"/>';
                        break;
                    case 3:
                        var inputHidden = '<input class="holidayOverNight" name="holidayOverNight'+imgIndex+'" type="hidden" value="'+minuteTime+'" />';
                        var inputTextH = '<input class="holidayOverNightH" size="2" maxlength="2" style="text-align:right;" type="text" value="'+timeArr[0]+'"/>';
                        var inputTextM = '<input class="holidayOverNightM" size="2" maxlength="2" style="text-align:right;" type="text" value="'+timeArr[1]+'"/>';
                        break;
                }
                if (spanF) {
                    spanF.innerHTML = inputHidden + inputTextH + ' : ' + inputTextM;
                } else {
                    $(this).html( inputHidden + inputTextH + ' : ' + inputTextM );
                }
            });//end each


            /* Event binding*/
            $parentTr.find('.workdayCommonH, .workdayCommonM').blur(function(){
                var $parent = $(this).parent();
                var workdayCommonH = $parent.find('.workdayCommonH').val();
                var workdayCommonM = $parent.find('.workdayCommonM').val();

                $parent.find('.workdayCommon').val( workdayCommonH*60 + workdayCommonM*1 );
            }).keydown(function(e){
//                if ( (e.keyCode >= 49 && e.keyCode <= 57) || (e.keyCode >=96 && e.keyCode <=105) || e.keyCode ==8 || e.keyCode ==9 || e.keyCode ==46 || (e.keyCode >= 37 && e.keyCode <=40)) {
                if ( (e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >=96 && e.keyCode <=105) || e.keyCode ==8 || e.keyCode ==9 || e.keyCode ==46 || (e.keyCode >= 37 && e.keyCode <=40)) {
                    return true;
                } else {
                    return false;
                }
            });

            $parentTr.find('.workdayOverNightH, .workdayOverNightM').blur(function(){
                var $parent = $(this).parent();
                var workdayOverNightH = $parent.find('.workdayOverNightH').val();
                var workdayOverNightM = $parent.find('.workdayOverNightM').val();

                $parent.find('.workdayOverNight').val( workdayOverNightH*60 + workdayOverNightM*1 );
            }).keydown(function(e){
//                if ( (e.keyCode >= 49 && e.keyCode <= 57) || (e.keyCode >=96 && e.keyCode <=105) || e.keyCode ==8 || e.keyCode ==9 || e.keyCode ==46 || (e.keyCode >= 37 && e.keyCode <=40)) {
                if ( (e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >=96 && e.keyCode <=105) || e.keyCode ==8 || e.keyCode ==9 || e.keyCode ==46 || (e.keyCode >= 37 && e.keyCode <=40)) {
                    return true;
                } else {
                    return false;
                }
            });

            $parentTr.find('.holidayCommonH, .holidayCommonM').blur(function(){
                var $parent = $(this).parent();
                var holidayCommonH = $parent.find('.holidayCommonH').val();
                var holidayCommonM = $parent.find('.holidayCommonM').val();
                $parent.find('.holidayCommon').val( holidayCommonH*60 + holidayCommonM*1 );
            }).keydown(function(e){
//                if ( (e.keyCode >= 49 && e.keyCode <= 57) || (e.keyCode >=96 && e.keyCode <=105) || e.keyCode ==8 || e.keyCode ==9 || e.keyCode ==46 || (e.keyCode >= 37 && e.keyCode <=40)) {
                if ( (e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >=96 && e.keyCode <=105) || e.keyCode ==8 || e.keyCode ==9 || e.keyCode ==46 || (e.keyCode >= 37 && e.keyCode <=40)) {
                    return true;
                } else {
                    return false;
                }
            });

            $parentTr.find('.holidayOverNightH, .holidayOverNightM').blur(function(){
                var $parent = $(this).parent();
                var holidayOverNightH = $parent.find('.holidayOverNightH').val();
                var holidayOverNightM = $parent.find('.holidayOverNightM').val();
                $parent.find('.holidayOverNight').val( holidayOverNightH*60 + holidayOverNightM*1 );
            }).keydown(function(e){
//                if ( (e.keyCode >= 49 && e.keyCode <= 57) || (e.keyCode >=96 && e.keyCode <=105) || e.keyCode ==8 || e.keyCode ==9 || e.keyCode ==46 || (e.keyCode >= 37 && e.keyCode <=40)) {
                if ( (e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >=96 && e.keyCode <=105) || e.keyCode ==8 || e.keyCode ==9 || e.keyCode ==46 || (e.keyCode >= 37 && e.keyCode <=40)) {
                    return true;
                } else {
                    return false;
                }
            });
            /* end Event Bind*/
        }

    });

    /* for countTime*/
    $('.timeCountTd').each(function(index,elm){
        var $this = $(this);
        var firstSpan = $this.find('a:first span')[0];
        if ( firstSpan ) {
            var workdayCommonTd = $this.parent().find('.workdayCommonTd');
            var workdayOverNightTd = $this.parent().find('.workdayOverNightTd');
            var holidayCommonTd = $this.parent().find('.holidayCommonTd');
            var holidayOverNightTd = $this.parent().find('.holidayOverNightTd');

            var tds = [workdayCommonTd, workdayOverNightTd, holidayCommonTd, holidayOverNightTd];
            var allTimeCount = 0;
            $(tds).each(function(){
                var spanF = ($(this).find('span:first'))[0];
                if (spanF) {
                    var spanContent = spanF.innerHTML;
                    var spanContentArr = spanContent.split(':');
                    allTimeCount += spanContentArr[0]*60 + spanContentArr[1]*1;
                }
            });
            var hour = '';
            var minute = '';

            if (allTimeCount<60) {
                hour = '00';
                minute = allTimeCount;
            } else {
                hour = Math.floor(allTimeCount/60);
                minute = allTimeCount - hour*60;
                if (hour<10) {
                    hour = '0'+hour;
                }
                if (minute<10) {
                    minute = '0'+minute;
                }
            }
            firstSpan.innerHTML = hour+':'+minute;
        }
    });

    /* for zhenxiu */
    $('.zhenxiu').keydown(function(e){
        if ( (e.keyCode >= 49 && e.keyCode <= 57) || (e.keyCode >=96 && e.keyCode <=105) || e.keyCode ==8 || e.keyCode ==9 || e.keyCode ==46 || (e.keyCode >= 37 && e.keyCode <=40)) {
            return true;
        } else {
            return false;
        }
    });

    /*
    .blur(function(){
        var $parent = $(this).parent();
        var zhenxiuY = $parent.find('.zhenxiuY').val();
        var zhenxiuM = $parent.find('.zhenxiuM').val();
        $parent.find('.zhenxiu').val( zhenxiuY + zhenxiuM );
    })
    */

/*
    $('.timeCountH, .timeCountM').blur(function(){
        var $parent = $(this).parent();
        var timeCountH = $parent.find('.timeCountH').val();
        var timeCountM = $parent.find('.timeCountM').val();
        $parent.find('.timeCount').val( timeCountH + timeCountM );
    });
    */

    $('.workdayCommonH, .workdayCommonM').blur(function(){
        var $parent = $(this).parent();
        var workdayCommonH = $parent.find('.workdayCommonH').val();
        var workdayCommonM = $parent.find('.workdayCommonM').val();

        $parent.find('.workdayCommon').val( workdayCommonH*60 + workdayCommonM*1 );
    }).keydown(function(e){
//       if ( (e.keyCode >= 49 && e.keyCode <= 57) || (e.keyCode >=96 && e.keyCode <=105) || e.keyCode ==8 || e.keyCode ==9 || e.keyCode ==46 || (e.keyCode >= 37 && e.keyCode <=40)) {
       if ( (e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >=96 && e.keyCode <=105) || e.keyCode ==8 || e.keyCode ==9 || e.keyCode ==46 || (e.keyCode >= 37 && e.keyCode <=40)) {
            return true;
        } else {
            return false;
        }
    });

    $('.workdayOverNightH, .workdayOverNightM').blur(function(){
        var $parent = $(this).parent();
        var workdayOverNightH = $parent.find('.workdayOverNightH').val();
        var workdayOverNightM = $parent.find('.workdayOverNightM').val();

        $parent.find('.workdayOverNight').val( workdayOverNightH*60 + workdayOverNightM*1 );
    }).keydown(function(e){
//        if ( (e.keyCode >= 49 && e.keyCode <= 57) || (e.keyCode >=96 && e.keyCode <=105) || e.keyCode ==8 || e.keyCode ==9 || e.keyCode ==46 || (e.keyCode >= 37 && e.keyCode <=40)) {
        if ( (e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >=96 && e.keyCode <=105) || e.keyCode ==8 || e.keyCode ==9 || e.keyCode ==46 || (e.keyCode >= 37 && e.keyCode <=40)) {
            return true;
        } else {
            return false;
        }
    });

    $('.holidayCommonH, .holidayCommonM').blur(function(){
        var $parent = $(this).parent();
        var holidayCommonH = $parent.find('.holidayCommonH').val();
        var holidayCommonM = $parent.find('.holidayCommonM').val();
        $parent.find('.holidayCommon').val( holidayCommonH*60 + holidayCommonM*1 );
    }).keydown(function(e){
//        if ( (e.keyCode >= 49 && e.keyCode <= 57) || (e.keyCode >=96 && e.keyCode <=105) || e.keyCode ==8 || e.keyCode ==9 || e.keyCode ==46 || (e.keyCode >= 37 && e.keyCode <=40)) {
        if ( (e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >=96 && e.keyCode <=105) || e.keyCode ==8 || e.keyCode ==9 || e.keyCode ==46 || (e.keyCode >= 37 && e.keyCode <=40)) {
            return true;
        } else {
            return false;
        }
    });

    $('.holidayOverNightH, .holidayOverNightM').blur(function(){
        var $parent = $(this).parent();
        var holidayOverNightH = $parent.find('.holidayOverNightH').val();
        var holidayOverNightM = $parent.find('.holidayOverNightM').val();
        $parent.find('.holidayOverNight').val( holidayOverNightH*60 + holidayOverNightM*1 );
    }).keydown(function(e){
//        if ( (e.keyCode >= 49 && e.keyCode <= 57) || (e.keyCode >=96 && e.keyCode <=105) || e.keyCode ==8 || e.keyCode ==9 || e.keyCode ==46 || (e.keyCode >= 37 && e.keyCode <=40)) {
        if ( (e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >=96 && e.keyCode <=105) || e.keyCode ==8 || e.keyCode ==9 || e.keyCode ==46 || (e.keyCode >= 37 && e.keyCode <=40)) {
            return true;
        } else {
            return false;
        }
    });

    /* for all Count*/

    var $holidayComCount = $('#holidayComCountTd');
    $holidayComCount.data('colCount', 0);
    $('.holidayCommonTd').each(function(){
        colCount($(this), $holidayComCount)
    });
    //$holidayComCount[0].innerHTML = countTheReault($holidayComCount.data('colCount'));

    var $holidayNightCount = $('#holidayNightCountTd');
    $holidayNightCount.data('colCount', 0);
    $('.holidayOverNightTd').each(function(){
        colCount($(this), $holidayNightCount)
    });
    //$holidayNightCount[0].innerHTML = countTheReault($holidayNightCount.data('colCount'));

    var $workdayComCount = $('#workdayComCountTd');
    $workdayComCount.data('colCount', 0);
    $('.workdayCommonTd').each(function(){
        colCount($(this), $workdayComCount)
    });
    //$workdayComCount[0].innerHTML = countTheReault($workdayComCount.data('colCount'));

    var $workdayNightCount = $('#workdayNightCountTd');
    $workdayNightCount.data('colCount', 0);
    $('.workdayOverNightTd').each(function(){
        colCount($(this), $workdayNightCount)
    });
    //$workdayNightCount[0].innerHTML = countTheReault($workdayNightCount.data('colCount'));


    var $allCountTd = $('#allCountTd');
    var allTimeCount = $holidayComCount.data('colCount') + $holidayNightCount.data('colCount') +
        $workdayComCount.data('colCount') + $workdayNightCount.data('colCount');
    //$allCountTd[0].innerHTML = countTheReault(allTimeCount);



    function colCount($td,$resultTd) {
        var firstSpan = $td.find('span:first')[0];
        if ( firstSpan ) {
            var spanContent = $.trim(firstSpan.innerHTML);
            var timeSplit = spanContent.split(':');
            var colCount = timeSplit[0]*60 + timeSplit[1]*1;
            var lastCount = $resultTd.data('colCount');
            $resultTd.data('colCount', colCount + lastCount);
        }
    }

    function countTheReault($count) {
        $hour = '00';
        $minute = '00';
        if ( $count < 60) {
            $minute = ($count<10) ? '0'+$count : $count;
        } else {
            $hour = Math.floor($count/60);
            $minute = $count - $hour*60;
            $hour = ($hour<10) ? '0'+$hour : $hour;
            $minute = ($minute<10) ? '0'+$minute : $minute;
        }
        return $hour+':'+$minute;
    }
});

function validateNum(eventObj) {
//    if ( (eventObj.keyCode >= 49 && eventObj.keyCode <= 57) || (eventObj.keyCode >=96 && eventObj.keyCode <=105) || eventObj.keyCode ==8 || eventObj.keyCode ==9 || eventObj.keyCode ==46 || (eventObj.keyCode >= 37 && eventObj.keyCode <=40)) {
    if ( (eventObj.keyCode >= 48 && eventObj.keyCode <= 57) || (eventObj.keyCode >=96 && eventObj.keyCode <=105) || eventObj.keyCode ==8 || eventObj.keyCode ==9 || eventObj.keyCode ==46 || (eventObj.keyCode >= 37 && eventObj.keyCode <=40)) {
            return true;
        } else {
            return false;
        }
}